package top.infopub.mgr.bpa.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.infopub.enums.bpa.BpaProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.InnerAgrtDao;
import top.infopub.mgr.bpa.dao.InnerSuppleDao;
import top.infopub.mgr.bpa.domain.BpaProjectDomain;
import top.infopub.mgr.bpa.domain.InnerSuppleDomain;
import top.infopub.mgr.bpa.jsonVo.InnerAgrtJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.InnerSuppleService;
import top.infopub.mgr.bpa.vo.InnerSuppleVo;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.mkt.dao.ProjectAttachmentDao;
import top.infopub.model.bpa.*;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.util.IdFactory;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@Service
public class InnerSuppleServiceImpl extends GenericServiceImpl<InnerSupple, String> implements
		InnerSuppleService {

	@Autowired
	private InnerSuppleDao innerSuppleDao;

	@Autowired
    private InnerAgrtDao innerAgrtDao;

	@Autowired
    private ProjectAttachmentDao projectAttachmentDao;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;

	@Override
	public GenericDao<InnerSupple, String> getDao() {
		return innerSuppleDao;
	}

    @Override
    public Page<InnerSuppleDomain> selectByDomainWhere(Page<InnerSuppleDomain> page,
													   InnerSuppleVo innerSuppleVo) throws ParseException {
        //处理时间范围
        if (!StringUtils.isEmpty(innerSuppleVo.getDates()))
        {
            String[] splitTime = innerSuppleVo.getDates().split("~");
            SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
            innerSuppleVo.setStartDate(format.parse(splitTime[0]));
            innerSuppleVo.setEndDate(format.parse(splitTime[1]));
        }
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<InnerSuppleDomain>)innerSuppleDao.selectByDomainWhere(innerSuppleVo);
    }

    @Override
    public RspVo selectProject() {
        List<BpaProjectDomain> bpaProjectDomains =  innerSuppleDao.selectProject();
        return RspVo.getSuccessResponse(bpaProjectDomains);
    }

    @Override
    public RspVo queryById(String id) {
        InnerAgrtJsonVo jsonVo = new InnerAgrtJsonVo();
        //主表信息
        InnerSupple innerSupple = innerSuppleDao.selectByPrimaryKey(id);
        //合同评审信息
        InnerAgrtSup innerAgrtSup = innerAgrtDao.selectSup(innerSupple.getId(),"2");
        //子项信息---待定
        List<InnerListing> innerListings = innerAgrtDao.selectListing(id,"2");
        //综合计划
        List<BpaCompPlan> compPlan = innerAgrtDao.selectPlan(innerSupple.getId(),"2");
        //附件信息
        List<ProjectAttachment> attachmentList = projectAttachmentDao.queryByWhere(innerSupple.getProCode(), ProjectFileTypeEnum.FILE_TYPE79.getCode(), null);
        jsonVo.setInnerSupple(innerSupple);
        jsonVo.setInnerAgrtSup(innerAgrtSup);
        jsonVo.setCompPlan(compPlan);
        jsonVo.setInnerListings(innerListings);
        jsonVo.setFileAtt(fileAttaListToArray(attachmentList));
        return  RspVo.getSuccessResponse(jsonVo);
    }

    @Override
    @Transactional
    public RspVo saveAll(Integer submitType, AuthorizationVO userDtl, InnerSupple innerSupple, List<InnerListing> innerListings, InnerAgrtSup innerAgrtSup, List<BpaCompPlan> compPlan, List<ProjectAttachment> attachmentList,String bodyStr) {
        //处理主表数据
        if (StringUtils.isNotBlank(innerSupple.getId())){
            innerSuppleDao.updateByPrimaryKey(innerSupple);
        }else{
            innerSuppleDao.insertById(innerSupple);
        }
        final String type = "2"; //合同评审
        //处理合同评审详细信息
        innerAgrtDao.deleteAgrtSup(innerSupple.getId(),type);
        innerAgrtSup.setInnerId(innerSupple.getId());
        innerAgrtSup.setInnerType(type);
        innerAgrtDao.insertAgrtSup(innerAgrtSup);

        //处理子项信息
        innerAgrtDao.deleteListing(innerSupple.getId(),type);
        int seq = 0;
        for (InnerListing innerListing : innerListings) {
            innerListing.setInnerId(innerSupple.getId());
            innerListing.setType(type);
            innerListing.setSeq(seq++);
        }
        innerAgrtDao.insertBatchListing(innerListings);
        //处理综合计划
        innerAgrtDao.deletePlan(innerSupple.getId(),type);
        for (BpaCompPlan bpaCompPlan : compPlan) {
            bpaCompPlan.setContractId(innerSupple.getId());
            bpaCompPlan.setType(type);
        }
        innerAgrtDao.insertBatchPlan(compPlan);

        //处理附件
        if (attachmentList!=null && attachmentList.size()>0){
            //批量插入附件信息
            projectAttachmentDao.deleteByWhere(innerSupple.getProCode(), ProjectFileTypeEnum.FILE_TYPE83.getCode(), null);
            for (ProjectAttachment attachment : attachmentList) {
                attachment.setProCode(innerSupple.getProCode());
            }
            projectAttachmentDao.insertBatch(attachmentList);
        }
        if (submitType==2){
            bpaProcessManageService.sendProcess(BpaProcessDetailTypeEnum.PRO_NODE_TYPE8, bodyStr, innerSupple.getId(), innerSupple.getProCode(), userDtl);
        }
        return RspVo.getSuccessResponse();
    }

    @Override
    public InnerSupple selectBySguid(String id) {
        return innerSuppleDao.selectByPrimaryKey(id);
    }


    public String[] fileAttaListToArray(List<ProjectAttachment> list) {
        if (list == null || list.size() <= 0) return null;
        String[] attaArr = new String[list.size()];
        int index = 0;
        for (ProjectAttachment pa : list) {
            StringBuffer tmp = new StringBuffer();
            tmp.append(pa.getSubTypes());
            tmp.append("|");
            tmp.append(pa.getFileName());
            tmp.append(pa.getFileSuffix());
            tmp.append("|");
            tmp.append(IdFactory.getUUIDSerialNumber());
            tmp.append("|");
            tmp.append(pa.getFilePath());
            attaArr[index] = tmp.toString();
            index++;
        }
        return attaArr;
    }
}
